#include <bits/stdc++.h>
using namespace std;
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
using ll = long long;
struct pp{
    ll l, r, v;
}a[100100];
ll n, k;
bool cmp(pp a,pp b)
{
    return a.l < b.l;
}

void solve()
{
    cin >>  n >> k;
    for(ll i=1;i<=n;i++)
    {
        ll l,r,v;
        cin >> l >> r >> v;
        a[i] = {l, r, v};
    }
    sort(a + 1, a + 1 + n, cmp);

    for(ll i=1;i<=n;i++)
    {
        if(a[i].l<=k&&k<a[i].v)
        {
            k = a[i].v;
        }
    }
    cout << k << endl;
}
int main()
{
    int t = 1;
    cin >> t;
    while(t--)
    {
        solve();
    }
   return 0;
}
